<?php
/**
 * Created by PhpStorm.
 * User: LOPO
 * Date: 2020/4/17
 * Time: 11:29
 */
namespace App\HttpController\Api;
use App\HttpController\Api\Base;
use App\Model\Video as VideoModel;
use EasySwoole\Http\Message\Status;
use EasySwoole\Validate\Validate;
use  EasySwoole\EasySwoole\Logger;

/**
 * Class Video
 * @package App\HttpController\Api
 */
class Video extends Base{
    public $logType = "video:";
    public function add(){
        $params =$this->request()->getRequestParam();
        Logger::getInstance()->log($this->logType.'add:'.json_encode($params),Logger::LOG_LEVEL_INFO,'DEBUG');
        $ruleObj = new Validate();
        $ruleObj->addColumn('name', '名字不为空')->required('名字不为空');
        $ruleObj->addColumn('url', '连接')->required('连接不为空')->lengthMin(3,'最小长度不小于3位');
        $bool = $ruleObj->validate($params);
        if(!$bool){
            return $this->writeJson(Status::CODE_BAD_REQUEST,"{$ruleObj->getError()->getErrorRuleMsg()}");
        }
        $data = [
            'name'=>$params['name'],
            'url'=>$params['url'],
            'image'=>$params['image'],
            'content'=>$params['content'],
            'cat_id'=>$params['cat_id'],
            'create_time'=>time(),
            'uploader'=>'lopo',
            'status'=>\Yaconf::get("status.normal"),
        ];

        //插入
        try{
            $modelObj = new VideoModel();
            $videoId = $modelObj->add($data);
        }catch (\Exception $e){
            return $this->writeJson(Status::CODE_BAD_REQUEST,$e->getMessage());
        }
        if (!empty($videoId)){
            return $this->writeJson(Status::CODE_OK,"ok",['id'=>$videoId]);
        }else{
            return $this->writeJson(Status::CODE_BAD_REQUEST,'提交视频有误',['id'=>0]);
        }

    }
}